package com.wangjiyuan.giftshuo.ui.fragment.classify.strategy.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.bean.classifybean.strategy.ColumnBean;
import com.wangjiyuan.giftshuo.bean.classifybean.strategy.GroupBean;
import com.wangjiyuan.giftshuo.config.classify.ClassifyConstant;
import com.wangjiyuan.giftshuo.ui.activity.classify.strategy_details.group_all_details.StrategyDetailsActivity;
import com.wangjiyuan.giftshuo.ui.activity.classify.strategy_details.group_details.GroupDetActivity;
import com.wangjiyuan.giftshuo.utils.RecyclerViewAdapterHelper;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.wangjiyuan.giftshuo.config.classify.ClassifyConstant.Strategy_MAP_ID;
import static com.wangjiyuan.giftshuo.config.classify.ClassifyConstant.Strategy_MAP_NAME;
import static com.wangjiyuan.giftshuo.config.classify.ConstantLength.LAYOUT_PADDING;
import static com.wangjiyuan.giftshuo.config.classify.ConstantLength.LAYOUT_PADDING_RIGHT;
import static com.wangjiyuan.giftshuo.config.classify.ConstantLength.IMAGEVIEW_PADDING_HEIGH;
import static com.wangjiyuan.giftshuo.config.classify.ConstantLength.IMAGEVIEW_PADDING_WIDTH;
import static com.wangjiyuan.giftshuo.config.classify.ConstantLength.TEXT_PADDING_HEIGH;

/**
 * Created by QXL on 2016/11/15.
 */

public class StrategyRecyclerViewAdapter extends RecyclerViewAdapterHelper<Object> {

    //    定义传递栏目位置的接口
    public interface IColumnPositionListener {
        void getColumnPosition(int position);
    }

    private IColumnPositionListener iColumnPositionListener;

    public static final int TYPE1 = 0, TYPE2 = 1;

    private int widthPixels;//屏幕宽度
    private int heightPixels;//屏幕高度

    private ColumnBean columnBean;

    public StrategyRecyclerViewAdapter(Context context, List<Object> list, ColumnBean columnBean,
                                       IColumnPositionListener iColumnPositionListener) {
        super(context, list);
        this.columnBean = columnBean;//构造传递头布局对象
        this.iColumnPositionListener = iColumnPositionListener;
    }

    /**
     * @param position TYPE1 头布局
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        if (mList.get(position) instanceof ColumnBean) {
            return TYPE1;
        } else {
            return TYPE2;
        }
    }

    /**
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case TYPE1:
                view = mInflater.inflate(R.layout.strategyitem1view, parent, false);
                return new ViewHolder1(view);
            case TYPE2:
                view = mInflater.inflate(R.layout.strategyitem2view, parent, false);
                return new ViewHolder2(view);
        }
        return null;
    }

    /**
     * @param holder
     * @param position
     */
    @Override
    public void onBindMyViewHolder(RecyclerView.ViewHolder holder, final int position) {
        widthPixels = mContext.getResources().getDisplayMetrics().widthPixels;
        heightPixels = mContext.getResources().getDisplayMetrics().heightPixels;
        if (holder instanceof ViewHolder2) {
//            绑定品类、风格、对象数据
            GroupBean.DataBean.ChannelGroupsBean bean =
                    (GroupBean.DataBean.ChannelGroupsBean) mList.get(position);

//           获得内容数量，如果小于6，查看全部不显示
            int size = bean.getChannels().size();
            if (size <= 6) {
                ((ViewHolder2) holder).textView_all_item2.setVisibility(View.GONE);
            }

            ((ViewHolder2) holder).textView_item2.setText(bean.getName());

            Glide.with(mContext).load(bean.getChannels().get(0).getCoverImageUrl())
                    .override(widthPixels / 2 - IMAGEVIEW_PADDING_WIDTH * 2 - LAYOUT_PADDING_RIGHT, heightPixels / 3)
                    .into(((ViewHolder2) holder).imageView1_item2);
            Glide.with(mContext).load(bean.getChannels().get(1).getCoverImageUrl())
                    .override(widthPixels / 2 - IMAGEVIEW_PADDING_WIDTH * 2 - LAYOUT_PADDING_RIGHT, heightPixels / 3)
                    .into(((ViewHolder2) holder).imageView2_item2);
            Glide.with(mContext).load(bean.getChannels().get(2).getCoverImageUrl())
                    .override(widthPixels / 2 - IMAGEVIEW_PADDING_WIDTH * 2 - LAYOUT_PADDING_RIGHT, heightPixels / 3)
                    .into(((ViewHolder2) holder).imageView3_item2);
            Glide.with(mContext).load(bean.getChannels().get(3).getCoverImageUrl())
                    .override(widthPixels / 2 - IMAGEVIEW_PADDING_WIDTH * 2 - LAYOUT_PADDING_RIGHT, heightPixels / 3)
                    .into(((ViewHolder2) holder).imageView4_item2);
            Glide.with(mContext).load(bean.getChannels().get(4).getCoverImageUrl())
                    .override(widthPixels / 2 - IMAGEVIEW_PADDING_WIDTH * 2 - LAYOUT_PADDING_RIGHT, heightPixels / 3)
                    .into(((ViewHolder2) holder).imageView5_item2);
            Glide.with(mContext).load(bean.getChannels().get(5).getCoverImageUrl())
                    .override(widthPixels / 2 - IMAGEVIEW_PADDING_WIDTH * 2 - LAYOUT_PADDING_RIGHT, heightPixels / 3)
                    .into(((ViewHolder2) holder).imageView6_item2);

            ((ViewHolder2) holder).checkPosition(position);
        }
//        绑定头布局数据
        else {
            GridLayoutManager gridLayout = new GridLayoutManager(mContext, 3, OrientationHelper.HORIZONTAL, false);
            List<Object> objects = new ArrayList<>();
            for (int i = 0; i < 11; i++) {
                ColumnBean.DataBean data = columnBean.getData();
                objects.add(data);
            }
            ColumnBean.DataBean.PagingBean paging = columnBean.getData().getPaging();
            objects.add(paging);

            ColumnRecyclerAdapter adapter = new ColumnRecyclerAdapter(mContext, objects);
            ((ViewHolder1) holder).recyclerView_item1.setHasFixedSize(true);
            ((ViewHolder1) holder).recyclerView_item1.setLayoutManager(gridLayout);
            ((ViewHolder1) holder).recyclerView_item1.setAdapter(adapter);

            ((ViewHolder1) holder).textView_item1_all.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                接口回调
                    iColumnPositionListener.getColumnPosition(position);
                }
            });


        }
    }


    //    头布局
    class ViewHolder1 extends RecyclerView.ViewHolder {
        LinearLayout linearLayout_item1;
        TextView textView_item1_column, textView_item1_all;
        RecyclerView recyclerView_item1;

        public ViewHolder1(View itemView) {
            super(itemView);
            linearLayout_item1 = (LinearLayout) itemView.findViewById(R.id.linearLayout_item1);
            linearLayout_item1.setPadding(LAYOUT_PADDING, LAYOUT_PADDING, LAYOUT_PADDING, LAYOUT_PADDING_RIGHT);

            textView_item1_column = (TextView) itemView.findViewById(R.id.textView_item1_column);
            textView_item1_column.setPadding(LAYOUT_PADDING + IMAGEVIEW_PADDING_WIDTH, LAYOUT_PADDING, LAYOUT_PADDING, LAYOUT_PADDING_RIGHT);

            textView_item1_all = (TextView) itemView.findViewById(R.id.textView_item1_all);
            textView_item1_all.setPadding(LAYOUT_PADDING + IMAGEVIEW_PADDING_WIDTH, TEXT_PADDING_HEIGH,
                    LAYOUT_PADDING_RIGHT + IMAGEVIEW_PADDING_WIDTH, TEXT_PADDING_HEIGH);
            recyclerView_item1 = (RecyclerView) itemView.findViewById(R.id.recyclerView_item1);
        }
    }

    //    品类、风格、对象布局
    class ViewHolder2 extends RecyclerView.ViewHolder implements View.OnClickListener{
        GridLayout gridLayout_item2;
        TextView textView_item2, textView_all_item2;
        ImageView imageView1_item2, imageView2_item2,
                imageView3_item2, imageView4_item2, imageView5_item2, imageView6_item2;

        public ViewHolder2(View itemView) {
            super(itemView);
            gridLayout_item2 = (GridLayout) itemView.findViewById(R.id.gridLayout_item2);
            gridLayout_item2.setPadding(LAYOUT_PADDING, LAYOUT_PADDING, LAYOUT_PADDING, LAYOUT_PADDING_RIGHT);

            textView_item2 = (TextView) itemView.findViewById(R.id.text_item2);
            textView_item2.setPadding(LAYOUT_PADDING + IMAGEVIEW_PADDING_WIDTH, TEXT_PADDING_HEIGH,
                    LAYOUT_PADDING_RIGHT + IMAGEVIEW_PADDING_WIDTH, TEXT_PADDING_HEIGH);

            textView_all_item2 = (TextView) itemView.findViewById(R.id.text_all_item2);
            textView_all_item2.setPadding(LAYOUT_PADDING + IMAGEVIEW_PADDING_WIDTH, TEXT_PADDING_HEIGH,
                    LAYOUT_PADDING_RIGHT + IMAGEVIEW_PADDING_WIDTH, TEXT_PADDING_HEIGH);

            imageView1_item2 = (ImageView) itemView.findViewById(R.id.imageView1_item2);
            imageView1_item2.setPadding(IMAGEVIEW_PADDING_WIDTH, IMAGEVIEW_PADDING_HEIGH,
                    IMAGEVIEW_PADDING_WIDTH, IMAGEVIEW_PADDING_HEIGH);

            imageView2_item2 = (ImageView) itemView.findViewById(R.id.imageView2_item2);
            imageView2_item2.setPadding(IMAGEVIEW_PADDING_WIDTH, IMAGEVIEW_PADDING_HEIGH,
                    IMAGEVIEW_PADDING_WIDTH, IMAGEVIEW_PADDING_HEIGH);

            imageView3_item2 = (ImageView) itemView.findViewById(R.id.imageView3_item2);
            imageView3_item2.setPadding(IMAGEVIEW_PADDING_WIDTH, IMAGEVIEW_PADDING_HEIGH,
                    IMAGEVIEW_PADDING_WIDTH, IMAGEVIEW_PADDING_HEIGH);

            imageView4_item2 = (ImageView) itemView.findViewById(R.id.imageView4_item2);
            imageView4_item2.setPadding(IMAGEVIEW_PADDING_WIDTH, IMAGEVIEW_PADDING_HEIGH,
                    IMAGEVIEW_PADDING_WIDTH, IMAGEVIEW_PADDING_HEIGH);

            imageView5_item2 = (ImageView) itemView.findViewById(R.id.imageView5_item2);
            imageView5_item2.setPadding(IMAGEVIEW_PADDING_WIDTH, IMAGEVIEW_PADDING_HEIGH,
                    IMAGEVIEW_PADDING_WIDTH, IMAGEVIEW_PADDING_HEIGH);

            imageView6_item2 = (ImageView) itemView.findViewById(R.id.imageView6_item2);
            imageView6_item2.setPadding(IMAGEVIEW_PADDING_WIDTH, IMAGEVIEW_PADDING_HEIGH,
                    IMAGEVIEW_PADDING_WIDTH, IMAGEVIEW_PADDING_HEIGH);

            textView_all_item2.setOnClickListener(this);
            imageView1_item2.setOnClickListener(this);
            imageView2_item2.setOnClickListener(this);
            imageView3_item2.setOnClickListener(this);
            imageView4_item2.setOnClickListener(this);
            imageView5_item2.setOnClickListener(this);
            imageView6_item2.setOnClickListener(this);
        }
        private int mPosition;
        public void checkPosition(int itemPosition){
            this.mPosition=itemPosition;
        }

        @Override
        public void onClick(View v) {
            GroupBean.DataBean.ChannelGroupsBean groups = (GroupBean.DataBean.ChannelGroupsBean) mList.get(mPosition);
            Bundle bundle=null;
            String name=null;
            Map<String,String> map;
            Intent intentID= new Intent(mContext, GroupDetActivity.class);
            int ID=0;
            switch (v.getId()){
                case R.id.text_all_item2:
//                    通过bundle传递对象
                    Intent intent = new Intent(mContext, StrategyDetailsActivity.class);
                    bundle = new Bundle();
                    bundle.putSerializable(ClassifyConstant.GROUPS, groups);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                    break;

                case R.id.imageView1_item2:
                    name = groups.getChannels().get(0).getName();
                    ID=groups.getChannels().get(0).getId();
                    Log.e("test","name=="+name);
                    Log.e("test","ID=="+ID);
                    map=new HashMap<>();
                    map.put(Strategy_MAP_NAME,name);
                    map.put(Strategy_MAP_ID,ID+"");
//                    粘性的eventBus传值
                    EventBus.getDefault().postSticky(map);
                    mContext.startActivity(intentID);
                    break;
                case R.id.imageView2_item2:
                    name = groups.getChannels().get(1).getName();
                    ID=groups.getChannels().get(1).getId();
                    Log.e("test","ID=="+ID);
                    map=new HashMap<>();
                    map.put(Strategy_MAP_NAME,name);
                    map.put(Strategy_MAP_ID,ID+"");
//                    粘性的eventBus传值
                    EventBus.getDefault().postSticky(map);
                    mContext.startActivity(intentID);
                    break;
                case R.id.imageView3_item2:
                    name = groups.getChannels().get(2).getName();
                    ID=groups.getChannels().get(2).getId();
                    Log.e("test","name=="+name);
                    Log.e("test","ID=="+ID);
                    map=new HashMap<>();
                    map.put(Strategy_MAP_NAME,name);
                    map.put(Strategy_MAP_ID,ID+"");
//                    粘性的eventBus传值
                    EventBus.getDefault().postSticky(map);
                    mContext.startActivity(intentID);
                    break;
                case R.id.imageView4_item2:
                    name = groups.getChannels().get(3).getName();
                    ID=groups.getChannels().get(3).getId();
                    Log.e("test","name=="+name);
                    Log.e("test","ID=="+ID);
                    map=new HashMap<>();
                    map.put(Strategy_MAP_NAME,name);
                    map.put(Strategy_MAP_ID,ID+"");
//                    粘性的eventBus传值
                    EventBus.getDefault().postSticky(map);
                    mContext.startActivity(intentID);
                    break;
                case R.id.imageView5_item2:
                    name = groups.getChannels().get(4).getName();
                    ID=groups.getChannels().get(4).getId();
                    Log.e("test","name=="+name);
                    Log.e("test","ID=="+ID);
                    map=new HashMap<>();
                    map.put(Strategy_MAP_NAME,name);
                    map.put(Strategy_MAP_ID,ID+"");
//                    粘性的eventBus传值
                    EventBus.getDefault().postSticky(map);
                    mContext.startActivity(intentID);
                    break;
                case R.id.imageView6_item2:
                    name = groups.getChannels().get(5).getName();
                    ID=groups.getChannels().get(5).getId();
                    Log.e("test","name=="+name);
                    Log.e("test","ID=="+ID);
                    map=new HashMap<>();
                    map.put(Strategy_MAP_NAME,name);
                    map.put(Strategy_MAP_ID,ID+"");
//                    粘性的eventBus传值
                    EventBus.getDefault().postSticky(map);
                    mContext.startActivity(intentID);
                    break;
            }
        }
    }
}
